//
//  shoppingCartViewController.m
//  giuseppe
//
//  Created by Apple on 15/7/13.
//  Copyright (c) 2015年 GiantUmbrella. All rights reserved.
//

#import "shoppingCartViewController.h"
#import "define.h"
#import "UIImageView+WebCache.h"
#import "submitOrderViewController.h"
#import "shoppingCartModel.h"


@interface shoppingCartViewController ()<UITableViewDataSource,UITableViewDelegate>{
    
    UITableView *_tableView;
    UIButton *_deleteButton;
    UIButton *_selectButton;
    UILabel *_totalPriceLabel;
    UIButton *allSelectedButton;
    BOOL allSelected;
    double total;
    
    NSMutableArray *shoppingCartModelArray;
    NSArray *dataArray;
}

@end

@implementation shoppingCartViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    allSelected = NO;
    
    //自定义的导航栏
    UIView *navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    navView.backgroundColor = [UIColor whiteColor];
    navView.alpha = 0.90;

    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(10, 33, 27, 16);
    [backButton setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth/2-50, 20, 100, 40)];
    titleLabel.text = @"购物车";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    
    [self loadData];
    [self _initView];
    [self _initToolBarView];

    [navView addSubview:backButton];
    [navView addSubview:titleLabel];
    [self.view addSubview:navView];

}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setToolbarHidden:NO animated:animated];
}


- (void)loadData{
    
    
    dataArray =@[
  @{@"imgUrl":@"http://image.big5.made-in-china.com/2f0j01YCyTGcsFbHul/%E6%A0%A1%E6%9C%8D.jpg",@"brief":@"乔治白",@"orderNumber":@"100",@"price":@"123",@"amount":@"123",@"id":@"123"},
  @{@"imgUrl":@"http://image.big5.made-in-china.com/2f0j01YCyTGcsFbHul/%E6%A0%A1%E6%9C%8D.jpg",@"brief":@"乔治白",@"orderNumber":@"100",@"price":@"123",@"amount":@"123",@"id":@"123"}
                          ];
    

    shoppingCartModelArray = [[NSMutableArray alloc]init];
    
    for (NSDictionary *dic in dataArray) {
        
        shoppingCartModel *model = [shoppingCartModel initWithDicAttributes:dic];
        [shoppingCartModelArray addObject:model];
        
    }

  
    
    
    
}

- (void)_initView{
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-self.navigationController.toolbar.frame.size.height)];
    _tableView.rowHeight = 150;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.allowsSelection = NO;
    

    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    headerView.backgroundColor = [UIColor whiteColor];
    
    
    _tableView.tableHeaderView = headerView;
    
    [self.view addSubview:_tableView];
    
    
}

- (void)_initToolBarView{
    
    
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, self.navigationController.toolbar.frame.size.height)];
    backView.backgroundColor = [UIColor whiteColor];
    
    UILabel *allSelectedLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 10, 50, 20)];
    allSelectedLabel.text = @"全选";
    
    
    UILabel *priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 10, 80, 20)];
    priceLabel.text = @"合计";
    
    UIButton *payButton = [UIButton buttonWithType:UIButtonTypeCustom];
    payButton.frame = CGRectMake(kScreenWidth-130, 5, 90, self.navigationController.toolbar.frame.size.height-10);
    payButton.backgroundColor = mainColor;
    [payButton setTitle:@"去结算" forState:UIControlStateNormal];
    payButton.titleLabel.adjustsFontSizeToFitWidth = YES;
    [payButton addTarget:self action:@selector(submitOrder) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    allSelectedButton = [UIButton buttonWithType:UIButtonTypeCustom];
    allSelectedButton.frame = CGRectMake(10, 10, 20, 20);
    allSelectedButton.tag = 10001;
    allSelectedButton.backgroundColor = [UIColor grayColor];
    [allSelectedButton addTarget:self action:@selector(allSelected:) forControlEvents:UIControlEventTouchUpInside];

    
    _totalPriceLabel = [[UILabel alloc]initWithFrame:CGRectMake(priceLabel.frame.origin.x+priceLabel.frame.size.width+5, 10, 80, 20)];
    _totalPriceLabel.text = @"0.00";
    _totalPriceLabel.textColor = mainColor;
    
    [self.navigationController.toolbar addSubview:backView];
    [self.navigationController.toolbar addSubview:allSelectedLabel];
    [self.navigationController.toolbar addSubview:priceLabel];
    [self.navigationController.toolbar addSubview:payButton];
    [self.navigationController.toolbar addSubview:allSelectedButton];
    [self.navigationController.toolbar addSubview:_totalPriceLabel];
    
    
    
}

#pragma mark - Table view data source && delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return shoppingCartModelArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *indentify = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentify];
//    if (!cell) {
    
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:indentify];
        
        
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 30, 200, 20)];

        titleLabel.textColor = [UIColor darkGrayColor];
        titleLabel.font = [UIFont systemFontOfSize:13];
        
        UILabel *orderIdentityLabel = [[UILabel alloc]initWithFrame:CGRectMake(140, 55, 150, 20)];
        orderIdentityLabel.textColor = [UIColor darkGrayColor];
        orderIdentityLabel.font = [UIFont systemFontOfSize:13];
        
        
        
        UIImageView *goodsImage = [[UIImageView alloc]initWithFrame:CGRectMake(50, 10, 80, 120)];

        
        UILabel *priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(140, 80, 70, 30)];
        priceLabel.textColor = mainColor;

    
        priceLabel.tag = indexPath.row + 200;
        
        UILabel *numberLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth-100, 85, 40, 20)];
        
        numberLabel.tag = indexPath.row +100;
        
        
        UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
        addButton.frame = CGRectMake(kScreenWidth-60, 85, 30, 30);
        addButton.tag = indexPath.row+50;
        addButton.backgroundColor = [UIColor redColor];
        [addButton addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];
        
        UIButton *reduceButton = [UIButton buttonWithType:UIButtonTypeCustom];
        reduceButton.frame = CGRectMake(kScreenWidth-150, 85, 30, 30);
        reduceButton.tag = indexPath.row+50;
        reduceButton.backgroundColor = [UIColor redColor];
        [reduceButton addTarget:self action:@selector(reduce:) forControlEvents:UIControlEventTouchUpInside];
        
        

        
        UIButton *clickButton = [UIButton buttonWithType:UIButtonTypeCustom];
        clickButton.frame = CGRectMake(10, 30, 20, 20);
    clickButton.tag = indexPath.row +50;
        [clickButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        
        if (allSelected) {
            clickButton.backgroundColor = mainColor;
        }else{
            clickButton.backgroundColor = [UIColor grayColor];
        }
        
        
        
        
        shoppingCartModel *tmp = shoppingCartModelArray[indexPath.row];


        [goodsImage  sd_setImageWithURL:[NSURL URLWithString:tmp.imgUrl] placeholderImage:[UIImage imageNamed:@"1"]];
        
        
        numberLabel.text = tmp.amount;
        numberLabel.font = [UIFont systemFontOfSize:13];
        titleLabel.text = tmp.brief;
        priceLabel.text = tmp.price;
        
        orderIdentityLabel.text = tmp.orderNumber;
        
        
        [cell.contentView addSubview:addButton];
        [cell.contentView addSubview:reduceButton];
        [cell.contentView addSubview:goodsImage];
        [cell.contentView addSubview:titleLabel];
        [cell.contentView addSubview:numberLabel];
        [cell.contentView addSubview:priceLabel];
        [cell.contentView addSubview:orderIdentityLabel];
        [cell.contentView addSubview:clickButton];
        
        
//    }
    
    return cell;
}

- (void)add:(UIButton *)button{

    UILabel *tmp = (UILabel *)[button.superview viewWithTag:(button.tag-50+100)];

    UILabel *priceLabel = (UILabel *)[button.superview viewWithTag:(button.tag-50+200)];
    double price = [priceLabel.text doubleValue];

    
    [tmp setText:[NSString stringWithFormat:@"%ld",[tmp.text integerValue]+1]];
    
    total = total + price;

    
    NSString *string = [NSString stringWithFormat:@"%f",total];
    
    _totalPriceLabel.text = string;

    
}

- (void)reduce:(UIButton *)button{


    UILabel *tmp = (UILabel *)[button.superview viewWithTag:(button.tag-50+100)];
    
    UILabel *priceLabel = (UILabel *)[button.superview viewWithTag:(button.tag-50+200)];
    double price = [priceLabel.text doubleValue];

    
    [tmp setText:[NSString stringWithFormat:@"%ld",[tmp.text integerValue]-1]];
    
    total = total - price;
    
    
    NSString *string = [NSString stringWithFormat:@"%f",total];
    
    _totalPriceLabel.text = string;

}


- (void)click:(UIButton *)button{
    
    if (button.backgroundColor == [UIColor grayColor]) {
        
        button.backgroundColor = mainColor;
        
        UILabel *numberLabel = (UILabel *)[button.superview viewWithTag:(button.tag-50+100)];
        NSLog(@"%@",numberLabel.text);
        double number = [numberLabel.text doubleValue];

        UILabel *priceLabel = (UILabel *)[button.superview viewWithTag:(button.tag-50+200)];
        double price = [priceLabel.text doubleValue];

        total = total + number*price;
        
        NSString *string = [NSString stringWithFormat:@"%f",total];
        
        _totalPriceLabel.text = string;
        
        
        }else{
            button.backgroundColor = [UIColor grayColor];
            
            UILabel *numberLabel = (UILabel *)[button.superview viewWithTag:(button.tag-50+100)];
            NSLog(@"%@",numberLabel.text);
            double number = [numberLabel.text doubleValue];
            
            UILabel *priceLabel = (UILabel *)[button.superview viewWithTag:(button.tag-50+200)];
            double price = [priceLabel.text doubleValue];
            
            total = total - number*price;
            
            NSString *string = [NSString stringWithFormat:@"%f",total];
            
            _totalPriceLabel.text = string;
            
        }
    
    


}

- (void)allSelected:(UIButton *)button{
    
    if (allSelected) {
        
        button.backgroundColor = [UIColor grayColor];
        allSelected = NO;
        [_tableView reloadData];
        
        for (NSDictionary *dic in dataArray) {
            
            NSString *numberString = [dic objectForKey:@"amount"];
            
            double number = [numberString doubleValue];
            
            NSString *priceString = [dic objectForKey:@"price"];
            double price = [priceString doubleValue];
            
            total = total - number*price;
            
            
        }
        
        NSString *string = [NSString stringWithFormat:@"%f",total];
        
        _totalPriceLabel.text = string;
    
    }else{
        
        button.backgroundColor = mainColor;
        allSelected = YES;
        [_tableView reloadData];
        
        
        
        for (NSDictionary *dic in dataArray) {
            
            NSString *numberString = [dic objectForKey:@"amount"];
            
            double number = [numberString doubleValue];
            
            NSString *priceString = [dic objectForKey:@"price"];
            double price = [priceString doubleValue];
            
            total = total + number*price;


        }
        
        NSString *string = [NSString stringWithFormat:@"%f",total];
        
        _totalPriceLabel.text = string;
    }

}

- (void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)submitOrder{
    
    submitOrderViewController *submitOrderVC = [[submitOrderViewController alloc]init];
    [self.navigationController pushViewController:submitOrderVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
